<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="Hexo Theme Keep">
    <meta name="description" content="Hexo Theme Keep">
    <meta name="author" content="SUNZJ">
    
    <title>
        
            Java基础01：基础语法 |
        
        SUNZJ的博客
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    <link rel="shortcut icon" href="/images/logo.svg">
    
<link rel="stylesheet" href="/css/font-awesome.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"sunzjya.gitee.io","root":"/","language":"zh"};
    KEEP.theme_config = {"toc":{"enable":false,"number":false,"expand_all":false,"init_open":false},"style":{"primary_color":"#0066CC","avatar":"/images/avatar.svg","favicon":"/images/logo.svg","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":false,"scale":false},"first_screen":{"enable":true,"background_img":"/images/bg.svg","description":"不吃学习的苦，就吃生活的苦！"},"scroll":{"progress_bar":{"enable":false},"percent":{"enable":false}}},"local_search":{"enable":false,"preload":false},"code_copy":{"enable":false,"style":"default"},"pjax":{"enable":false},"lazyload":{"enable":false},"version":"3.4.5"};
    KEEP.language_ago = {"second":"%s seconds ago","minute":"%s minutes ago","hour":"%s hours ago","day":"%s days ago","week":"%s weeks ago","month":"%s months ago","year":"%s years ago"};
  </script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
<div class="progress-bar-container">
    

    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                SUNZJ的博客
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/"
                            >
                                HOME
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/archives"
                            >
                                ARCHIVES
                            </a>
                        </li>
                    
                    
                </ul>
            </div>
            <div class="mobile">
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/">HOME</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/archives">ARCHIVES</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">Java基础01：基础语法</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="/images/avatar.svg">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">SUNZJ</span>
                        
                            <span class="author-label">Lv1</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;
        <span class="pc">2021-12-26 21:21:47</span>
        <span class="mobile">2021-12-26 21:21</span>
    </span>
    
    

    
    
    
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <p>[TOC]</p>
<h1 align="center">Java基础：基础语法</h1>

<p>Java 三大版本</p>
<ul>
<li>Java SE：标准版（控制台开发）</li>
<li>Java ME：嵌入式开发（手机，小家电）</li>
<li>Java EE：企业级开发（Web 端，服务器开发）</li>
</ul>
<h2 id="1、JDK、JRE、JVM-三者之间的关系"><a href="#1、JDK、JRE、JVM-三者之间的关系" class="headerlink" title="1、JDK、JRE、JVM 三者之间的关系"></a>1、JDK、JRE、JVM 三者之间的关系</h2><p>JDK（java development kit）</p>
<ul>
<li>JDK 是 Java 开发工具包，是整个 Java 的核心，包括了 Java 运行环境 JRE、Java 工具和 Java 基础类库。</li>
</ul>
<p>JRE（java runtime environment）</p>
<ul>
<li>JRE 是 Java 的运行环境，包括 JVM 标准实现及 Java 核心类库。</li>
</ul>
<p>JVM（java virtual machine）</p>
<ul>
<li>JVM 是 java 虚拟机，是整个 java 实现跨平台的最核心的部分，能够运行以 Java 语言写作的软件程序。</li>
</ul>
<p><strong>三者的关系</strong></p>
<ul>
<li>JDK = JRE + Java 开发工具</li>
<li>JRE = JVM + Java 基础类库</li>
</ul>
<p><strong>JDK 安装目录结构解析</strong></p>
<p><img src="Java%E5%9F%BA%E7%A1%8001%EF%BC%9A%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95.assets/1451576-20191009142214489-821867190.png" alt="img"></p>
<p>JVM：</p>
<p>　　JVM 即为 Java 虚拟机，它是 Java 跨平台实现的最核心的部分，所有的 Java 程序首先被编译成 .class 字节码文件，这种文件可以在 JVM 上解释执行，JVM 在执行字节码文件时，把其翻译成具体平台上的机器指令执行。（一次编译，到处运行）</p>
<p>JVM 执行程序的过程：</p>
<ol>
<li>加载 .class 字节码文件</li>
<li>管理并分配内存</li>
<li>执行垃圾收集</li>
</ol>
<p><img src="Java%E5%9F%BA%E7%A1%8001%EF%BC%9A%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95.assets/1451576-20191009142308500-541257647.png" alt="img"></p>
<h2 id="2、数据类型"><a href="#2、数据类型" class="headerlink" title="2、数据类型"></a>2、数据类型</h2><p>强类型语言：Java、C / C++……</p>
<ul>
<li>要求变量的使用要严格符合规定，所有变量必须先定义后才能使用。</li>
</ul>
<p>弱类型语言：Python、JavaScript、VBScript……</p>
<p><strong>Java 的数据类型分为两大类：</strong></p>
<p>1、基本数据类型（primitive type）（4类8种）</p>
<p>2、引用数据类型（reference type）</p>
<ul>
<li>类</li>
<li>接口</li>
<li>数组</li>
<li>……</li>
</ul>
<p><img src="Java%E5%9F%BA%E7%A1%8001%EF%BC%9A%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95.assets/Java%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B.png" alt="Java数据类型"></p>
<ol>
<li>整数型：byte、short、int、long</li>
<li>浮点型：float、double</li>
<li>字符型：char</li>
<li>布尔型：boolean</li>
</ol>
<p>1、整数：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 二进制 0b 十进制 八进制 0 十六进制 0x</span></span><br><span class="line"><span class="keyword">int</span> num1 = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> num2 = <span class="number">010</span>;</span><br><span class="line"><span class="keyword">int</span> num3 = <span class="number">0x10</span>;</span><br><span class="line"><span class="keyword">int</span> num4 = <span class="number">0b1100</span>;</span><br><span class="line"></span><br><span class="line">System.out.println(num1); <span class="comment">// 10</span></span><br><span class="line">System.out.println(num2); <span class="comment">// 8</span></span><br><span class="line">System.out.println(num3); <span class="comment">// 16</span></span><br><span class="line">System.out.println(num4); <span class="comment">// 12</span></span><br></pre></td></tr></table></figure>

<p>2、浮点数</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 浮点数扩展 银行业务怎么表示 钱</span></span><br><span class="line"><span class="comment">// BigDecimal 数学工具类</span></span><br><span class="line"><span class="comment">// float 有限 离散 舍入误差 大约 接近但不等于</span></span><br><span class="line"><span class="comment">// double</span></span><br><span class="line"><span class="comment">// 最好完全避免使用浮点数进行比较</span></span><br><span class="line"><span class="keyword">float</span> f1 = <span class="number">0.1f</span>; <span class="comment">// 0.1</span></span><br><span class="line"><span class="keyword">double</span> d1 = <span class="number">1.0</span> / <span class="number">10</span>; <span class="comment">// 0.1</span></span><br><span class="line">System.out.println(f1 == d1); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">float</span> f2 = <span class="number">123456789101f</span>;</span><br><span class="line"><span class="keyword">float</span> f3 = f2 + <span class="number">1</span>;</span><br><span class="line">System.out.println(f2 == f3); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<p>3、字符</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">char</span> ch1 = <span class="string">&#x27;A&#x27;</span>;</span><br><span class="line"><span class="keyword">char</span> ch2 = <span class="string">&#x27;中&#x27;</span>;</span><br><span class="line">System.out.println(ch1); <span class="comment">// A</span></span><br><span class="line">System.out.println((<span class="keyword">int</span>)ch1); <span class="comment">// 65 强制类型转换</span></span><br><span class="line">System.out.println(ch2); <span class="comment">// 中</span></span><br><span class="line">System.out.println((<span class="keyword">int</span>)ch2); <span class="comment">// 20013</span></span><br><span class="line"><span class="comment">// 所有字符本质是数字</span></span><br><span class="line"><span class="comment">// 编码表 Unicode 表：（97 = a 65 = A） 2 字节：0 - 65535</span></span><br><span class="line"><span class="keyword">char</span> ch3 = <span class="string">&#x27;\u0061&#x27;</span>;</span><br><span class="line">System.out.println(ch3); <span class="comment">// 6 * 16 + 1 -&gt; a</span></span><br></pre></td></tr></table></figure>

<p><code>注意事项：</code></p>
<ol>
<li>字符串不是基本类型，而是引用类型。</li>
<li>浮点型可能只是一个近似值，并非精确的值。</li>
<li>数据范围与字节数不一定相关，例如：float 数据范围比 long 更加广泛，但是 float 是 4 字节，long 是 8 字节。</li>
<li>浮点数当中默认类型是 double，如果一定要是用 float 类型，需要加上一个后缀 F / f。<br>如果是整数，默认为 int 类型，如果该一定要使用 long 类型，需要加上一个后缀 L / l。推荐使用大写字母。</li>
</ol>
<hr>
<h2 id="3、Java-中的-boolean-到底占用多少个字节？"><a href="#3、Java-中的-boolean-到底占用多少个字节？" class="headerlink" title="3、Java 中的 boolean 到底占用多少个字节？"></a>3、Java 中的 boolean 到底占用多少个字节？</h2><p>转载链接：<a class="link"   target="_blank" rel="noopener" href="http://blog.itpub.net/69917874/viewspace-2677032/" >http://blog.itpub.net/69917874/viewspace-2677032/<i class="fas fa-external-link-alt"></i></a></p>
<p>boolean 值只有 true 和 false 两种，这个数据类型只代表 1bit 的信息，但是它到底占了多少空间，却没有严格的说法，也就是说，不论它的所占空间是多少，只有1bit的信息是有意义的</p>
<p>在 Oracle 的 Java 虚拟机实现中，Java 语言中的 boolean 数组被编码成 Java 虚拟机的 byte 数组，每个元素占 8 比特。</p>
<p>Java 虚拟机中虽然定义了 boolean 类型，但是支持是很有限的，没有专门的虚拟机指令。</p>
<p>同时在 Java 语言中，对 boolean 值的操作被替换成 int 数据类型。</p>
<p>再去看看 Java 虚拟机规范上是怎么写的。</p>
<p>Java 虚拟机使用 1 表示 true ，0 表示 false 来编码 boolean 数组。</p>
<p>Java 语言的 boolean 值被编译器映射成 Java 虚拟机的 int 类型的时候，也是一样的</p>
<p>到这里，得出一个结论：</p>
<p><strong>boolean到底占几个字节，依赖于JVM</strong></p>
<p>第一，无论Boolean占用多少字节，只有1bit的值是有意义的</p>
<p>第二，boolean 类型被编译成 int 类型来使用，占 4 个 byte 。</p>
<p>第三，在 Java 虚拟机里，1 表示 true ，0 表示 false 。</p>
<hr>
<h2 id="4、数据类型转换"><a href="#4、数据类型转换" class="headerlink" title="4、数据类型转换"></a>4、数据类型转换</h2><p>由于 java 是强类型语言，所以要进行有些运算的时候，需要用到类型转换。</p>
<p><strong>当数据类型不一样的时候，将会发生数据类型装换。</strong></p>
<p>数据范围从小到大与它的字节数是不一定相关的。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 低--------------------------------------------------高</span></span><br><span class="line"><span class="keyword">byte</span>,<span class="keyword">short</span>,<span class="keyword">char</span> -&gt; <span class="keyword">int</span> -&gt; <span class="keyword">long</span> -&gt; <span class="keyword">float</span> -&gt; <span class="keyword">double</span></span><br></pre></td></tr></table></figure>

<p>运算中，不同类型的数据先转化为同一类型，然后进行计算</p>
<ul>
<li>强制类型转换</li>
<li>自动类型转换</li>
</ul>
<p><strong>1、自动类型转换（隐式）</strong></p>
<ul>
<li>特点：代码不需要进行特殊处理，自动完成。</li>
<li>规则：<strong>数据范围从小到大</strong><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// long --&gt; float,范围是float更大一些，符合从下到大的原则。</span></span><br><span class="line"><span class="keyword">float</span> num1 = <span class="number">1.</span><span class="number">2L</span>;</span><br></pre></td></tr></table></figure></li>
</ul>
<p><strong>2、强制类型转换（显式）</strong></p>
<ul>
<li>特点：代码需要进行特殊的格式处理，不能自动完成。</li>
<li>格式：范围小的类型 范围小的变量名 = (范围小的类型) 原本范围大的数据;</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> num1 = (<span class="keyword">int</span>) <span class="number">100L</span>;</span><br></pre></td></tr></table></figure>

<p><code>注意事项：</code></p>
<ol>
<li>在把高容量转换为低容量，有可能发生精度损失，数据溢出。</li>
<li>byte / short /char 这三种类型都可以发生数学运算，例如：加法“+”。</li>
<li>byte / short / char 这三种类型在运算的时候，都会被首先提升为 int 类型，然后再计算。</li>
<li><code>boolean 类型不能发生数据类型装换。</code></li>
<li>不能把对象类型转换为不相干的类型</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// long类型强制装换成int类型，数据溢出</span></span><br><span class="line"><span class="keyword">int</span> num1 = (<span class="keyword">int</span>)<span class="number">6000000000L</span>;</span><br><span class="line">System.out.println(num1); <span class="comment">// 1705032704</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// double --&gt; int 强制类型装换，精度损失</span></span><br><span class="line"><span class="keyword">int</span> num2 = (<span class="keyword">int</span>)<span class="number">3.99</span>;</span><br><span class="line"><span class="comment">// 这并不是四舍五入，所有的小数位都会被舍弃掉</span></span><br><span class="line">System.out.println(num2); <span class="comment">// 3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 计算机底层会用一个数字（二进制）来代表字符A，也就是65</span></span><br><span class="line"><span class="comment">// 一旦char类型进行了数学运算，那么字符就会按照一定的规则翻译成一个数字</span></span><br><span class="line"><span class="keyword">char</span> zifu1 = <span class="string">&#x27;A&#x27;</span>;</span><br><span class="line">System.out.println(zifu1 + <span class="number">1</span>); <span class="comment">// 66</span></span><br><span class="line"><span class="comment">// 注意：右侧数值大小不能超过左侧的类型范围</span></span><br><span class="line"><span class="keyword">byte</span> num3 = <span class="number">30</span>; </span><br><span class="line"><span class="keyword">byte</span> num4 = <span class="number">40</span>;</span><br><span class="line"><span class="comment">// byte + byte --&gt; int int --&gt; int </span></span><br><span class="line"><span class="comment">// byte result1 = num3 + num4; // 错误写法！</span></span><br><span class="line"><span class="keyword">int</span> result1 = num3 + num4; 	</span><br><span class="line">System.out.println(result1); <span class="comment">// 70</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">short</span> num5 = <span class="number">60</span>;</span><br><span class="line"><span class="comment">// byte + short --&gt; int + int --&gt; int</span></span><br><span class="line"><span class="comment">// int 强制装换成为 short：</span></span><br><span class="line"><span class="comment">// 注意必须保证逻辑上的真实大小本来就没有超过short范围，否则会发生数据溢出</span></span><br><span class="line"><span class="keyword">short</span> result2 = (<span class="keyword">short</span>)(num4 + num5);</span><br><span class="line">System.out.println(result2);</span><br><span class="line"></span><br><span class="line">System.out.println(<span class="string">&quot;=============================&quot;</span>)</span><br><span class="line"><span class="comment">//操作比较大的数的时候，注意溢出问题</span></span><br><span class="line"><span class="comment">//Jdk7新特性，数字只见可以用下划线分割</span></span><br><span class="line"><span class="keyword">int</span> i = <span class="number">1_000_000</span>;</span><br><span class="line">System.out.println(i); <span class="comment">// 1000000</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> money = <span class="number">10_0000_0000</span>;</span><br><span class="line"><span class="keyword">int</span> years = <span class="number">20</span>;</span><br><span class="line"><span class="keyword">int</span> total = money * years;<span class="comment">//-1474836480,计算的时候溢出了</span></span><br><span class="line"><span class="keyword">long</span> total2 = money * years;<span class="comment">//默认是int，转换之前已经存在问题了？</span></span><br><span class="line"><span class="keyword">long</span> total3 = money * ((<span class="keyword">long</span>) years);</span><br><span class="line"></span><br><span class="line">System.out.println(total); <span class="comment">// -1474836480</span></span><br><span class="line">System.out.println(total2); <span class="comment">// -1474836480</span></span><br><span class="line">System.out.println(total3); <span class="comment">// 20000000000</span></span><br><span class="line">System.out.println((<span class="keyword">long</span>)money * years); <span class="comment">// 20000000000</span></span><br></pre></td></tr></table></figure>



<h2 id="5、变量"><a href="#5、变量" class="headerlink" title="5、变量"></a>5、变量</h2><p>变量是什么：就是可以变化的量，在程序运行期间，内容可以发生改变的量。</p>
<p>java 是一种强类型语言，每个变量都必须声明其类型。</p>
<p>java 变量是程序中最基本的存储单元，其要素包括变量名，变量类型和作用域。</p>
<p>格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">数据类型 变量名称;</span><br><span class="line">变量名称 = 数据值;</span><br><span class="line"><span class="comment">// 或者</span></span><br><span class="line">数据类型 变量名称 = 数据值;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 右侧数值的范围不能超过左侧数据类型的取值范围，否则报错</span></span><br><span class="line"><span class="comment">// byte 是：-128 ~ 127</span></span><br><span class="line"><span class="keyword">byte</span> num1 = <span class="number">30</span>; <span class="comment">// 正确</span></span><br><span class="line"><span class="keyword">byte</span> num2 = <span class="number">300</span>; <span class="comment">// 错误</span></span><br><span class="line"><span class="comment">// float后缀F</span></span><br><span class="line"><span class="keyword">float</span> num3 = <span class="number">100F</span>;</span><br><span class="line"><span class="comment">// long后缀L</span></span><br><span class="line"><span class="keyword">long</span> num4 = <span class="number">200L</span>;</span><br></pre></td></tr></table></figure>

<p><code>注意事项：</code></p>
<p>1、如果在同一个作用域范围内（括号）创建多个变量，那么变量之间的名称不可以重复。</p>
<p>2、对于 float 和 long 类型来说，字母后缀 F 和 L 不要丢掉。</p>
<p>3、如果使用 byte 和 short 类型的变量，那么右侧的数字值不能超过左侧类型的取值范围。</p>
<p>4、局部变量一定要赋值之后，才能使用。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> i1;</span><br><span class="line">i1 = <span class="number">9</span>;</span><br><span class="line">System.out.println(i1);</span><br></pre></td></tr></table></figure>

<p>5、变量使用不能超过作用域范围。</p>
<ul>
<li>【作用域】：从定义变量的一行开始，一直到所属的大括号结束为止。</li>
</ul>
<p>6、可以通过一个语句来创建多个变量，但是一般情况不推荐这么写。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> i1, i2, i3;</span><br></pre></td></tr></table></figure>



<p><strong>变量的作用域</strong></p>
<ul>
<li>类变量</li>
<li>实例变量</li>
<li>局部变量</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 类变量 static</span></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">int</span> age;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">   实例变量类型 默认值</span></span><br><span class="line"><span class="comment">    byte        0</span></span><br><span class="line"><span class="comment">    short       0</span></span><br><span class="line"><span class="comment">    char        \u0000</span></span><br><span class="line"><span class="comment">    int         0</span></span><br><span class="line"><span class="comment">    long        0</span></span><br><span class="line"><span class="comment">    float       0.0</span></span><br><span class="line"><span class="comment">    double      0.0</span></span><br><span class="line"><span class="comment">    boolean     false</span></span><br><span class="line"><span class="comment">    引用类型    null</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">// 实例变量</span></span><br><span class="line">    <span class="keyword">double</span> money;</span><br><span class="line">    <span class="keyword">boolean</span> flag;</span><br><span class="line">    String name;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 临时变量 必须声明和初始化值</span></span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">10</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>常量</strong></p>
<p>常量（Constant）：初始化（initialize）后不能再改变值！不会变动的值。</p>
<p>所谓常量可以理解成一种特殊的变量，它的值被设定后，在程序运行过程中不允许被改变。</p>
<p>格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">final</span> 常量名 = 值;</span><br><span class="line"><span class="keyword">final</span> <span class="keyword">double</span> MAX_VALUE = <span class="number">127</span>;</span><br><span class="line"><span class="comment">// 修饰符，不区分先后顺序</span></span><br><span class="line"><span class="comment">// static final double PI = 3.14;</span></span><br><span class="line"><span class="keyword">final</span> <span class="keyword">static</span> <span class="keyword">double</span> PI = <span class="number">3.14</span>;</span><br></pre></td></tr></table></figure>

<ol>
<li>整型常量：123</li>
<li>浮点型常量：3.14，3.14f</li>
<li>字符常量：‘A’</li>
<li>布尔常量：true、false</li>
<li>字符串常量：“Hello,World”</li>
<li>空常量：null</li>
</ol>
<p><code>注意事项：不能直接打印空常量（null）</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">System.out.println(<span class="keyword">null</span>); <span class="comment">// 【错误】</span></span><br></pre></td></tr></table></figure>



<h2 id="6、运算符"><a href="#6、运算符" class="headerlink" title="6、运算符"></a>6、运算符</h2><ul>
<li>算术运算符</li>
<li>赋值运算符<ul>
<li>基本赋值运算符</li>
<li>扩展赋值运算符</li>
</ul>
</li>
<li>关系运算符</li>
<li>逻辑运算符</li>
<li>位运算符</li>
<li>条件运算符</li>
</ul>
<h3 id="6-1、算术运算符"><a href="#6-1、算术运算符" class="headerlink" title="6.1、算术运算符"></a>6.1、算术运算符</h3><blockquote>
<p>四则运算符及取模</p>
</blockquote>
<ul>
<li>对于一个整数表达式来说，除法用的是整数，整数除以整数，结果仍然是整数，只看商，不看余数。</li>
<li>只有对于整数的除法来说，取模运算符才有余数的意义。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">System.out.println(<span class="number">22.0</span> % <span class="number">2</span>); <span class="comment">// 0.0</span></span><br><span class="line">System.out.println(<span class="number">22.2</span> % <span class="number">2</span>); <span class="comment">// 0.1999999999999993</span></span><br></pre></td></tr></table></figure>

<p><code>注意事项：一旦运算符当中有不同的数据类型，那么结果将会是数据类型范围大的那种。（谁大听谁的）。</code></p>
<ul>
<li>四则运算当中的加号 “+” 有常见的三种用法：<ul>
<li>对于数值来说，那就是加法。</li>
</ul>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> a = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> b = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line">System.out.println(a + b); <span class="comment">// 30</span></span><br><span class="line">System.out.println(a - b); <span class="comment">// -10</span></span><br><span class="line">System.out.println(a * b); <span class="comment">// 200</span></span><br><span class="line">System.out.println(a / b); <span class="comment">// 0</span></span><br><span class="line">System.out.println(a / (<span class="keyword">double</span>)b); <span class="comment">// 0.5</span></span><br><span class="line">System.out.println(a % b); <span class="comment">// 10</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> l = <span class="number">1231280324389843L</span>;</span><br><span class="line"><span class="keyword">int</span> i = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">byte</span> b = <span class="number">127</span>;</span><br><span class="line"><span class="keyword">short</span> s = <span class="number">32767</span>;</span><br><span class="line"></span><br><span class="line">System.out.println(l + b + s + i); <span class="comment">// 1231280324422747 long </span></span><br><span class="line">System.out.println(b + s + i); <span class="comment">// 32904 int</span></span><br><span class="line">System.out.println(s + b); <span class="comment">// 32894 int</span></span><br></pre></td></tr></table></figure>
<ul>
<li>对于字符 char 类型来说，在计算之前，char 会被提升成为 int，然后再计算。</li>
</ul>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">char</span> ch = <span class="string">&#x27;A&#x27;</span>;</span><br><span class="line">System.out.println(ch + <span class="number">1</span>); <span class="comment">// 66</span></span><br><span class="line">System.out.println(ch + <span class="string">&#x27;0&#x27;</span>); <span class="comment">// 113 = 65 + 48</span></span><br></pre></td></tr></table></figure>
<ul>
<li>对于字符串String（首字母大写，不是关键字）来说，加号代表字符串 <strong>连接</strong> 操作。</li>
<li><strong>任何数据类型和字符串进行连接的时候，结果都会变成字符串</strong>。</li>
</ul>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> a = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> b = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 字符串连接符 + 和 String 类型 运算顺序 从左到右</span></span><br><span class="line">System.out.println(<span class="string">&quot;&quot;</span> + a + b); <span class="comment">// 1020</span></span><br><span class="line">System.out.println(a + b + <span class="string">&quot;&quot;</span>); </span><br><span class="line"><span class="comment">// 30</span></span><br></pre></td></tr></table></figure></li>
</ul>
<blockquote>
<p>自增自减运算符</p>
</blockquote>
<p>自增运算符：++、自减运算符：–</p>
<ul>
<li>基本含义：让一个变量涨一个数字1，或者让一个变量降一个数字1</li>
<li>使用格式：写在变量名称之前，或者写在变量名称之后，例如：++num，也可以num++</li>
<li>使用方式：<ol>
<li>单独使用：不和其他任何操作混合，自己独立成为一个步骤。</li>
<li>混合使用：和其他操作混合，例如与赋值混合，或者与打印混合，等。</li>
</ol>
</li>
<li>使用区别：<ol>
<li>在单独使用的时候，前++和后++没有任何区别，也就是：++num;和num++;是完全一样的。</li>
<li>在混合使用的时候，有【重大区别】<ol>
<li>前++：先加后用</li>
<li>后++：先用后加</li>
</ol>
</li>
</ol>
</li>
</ul>
<p><code>注意事项：只有变量才能使用自增、自减运算符。常量不可发生改变，所以不能用。</code></p>
<h3 id="6-2、赋值运算符"><a href="#6-2、赋值运算符" class="headerlink" title="6.2、赋值运算符"></a>6.2、赋值运算符</h3><ul>
<li>基本赋值运算符：就是一个等号 “=”。</li>
<li>复合赋值运算符：+=、-=、*=、/=、%=。</li>
</ul>
<p><code>注意事项：</code></p>
<ul>
<li>只有变量才能使用赋值运算符，常量不能进行赋值。</li>
<li>复合赋值运算符其中隐含了一个强制类型转换。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">byte</span> num = <span class="number">30</span>;</span><br><span class="line"><span class="comment">// num = num + 30;</span></span><br><span class="line"><span class="comment">// num = byte + int</span></span><br><span class="line"><span class="comment">// num = int + int</span></span><br><span class="line"><span class="comment">// num = int</span></span><br><span class="line"><span class="comment">// num = (byte)int;</span></span><br><span class="line">num += <span class="number">20</span>; </span><br></pre></td></tr></table></figure>



<h3 id="6-3、关系运算符"><a href="#6-3、关系运算符" class="headerlink" title="6.3、关系运算符"></a>6.3、关系运算符</h3><p><code>&gt;、&lt;、==、&gt;=、&lt;=、!=</code></p>
<p>注意事项：</p>
<ol>
<li>比较运算符的结果一定是个boolean值，成立就是true，不成立就是false。</li>
<li>如果进行多次判断，不能连着写。</li>
</ol>
<h3 id="6-4、逻辑运算符"><a href="#6-4、逻辑运算符" class="headerlink" title="6.4、逻辑运算符"></a>6.4、逻辑运算符</h3><ul>
<li>&amp;&amp; 一假则假</li>
<li>|| 一真则真</li>
<li>! 真则假 假则真</li>
</ul>
<p><code>注意事项：</code></p>
<ul>
<li><p>逻辑运算符只能用于boolean值。</p>
</li>
<li><p>逻辑与、逻辑或需要左右各一个boolean值，但是取反只要有唯一的一个boolean值即可。</p>
</li>
<li><p>逻辑与、逻辑或两种运算符，如果有多个条件，可以连续写。</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">boolean</span> a = <span class="keyword">false</span>;</span><br><span class="line"><span class="keyword">boolean</span> b = <span class="keyword">true</span>;</span><br><span class="line"></span><br><span class="line">System.out.println(<span class="string">&quot;a &amp;&amp; b：&quot;</span> + (a &amp;&amp; b)); <span class="comment">// false 一假则假</span></span><br><span class="line">System.out.println(<span class="string">&quot;a || b：&quot;</span> + (a || b)); <span class="comment">// true  一真则真</span></span><br><span class="line">System.out.println(<span class="string">&quot;!(a || b)：&quot;</span> + !(a || b)); <span class="comment">// false  真则假 假则真</span></span><br></pre></td></tr></table></figure>

<p>逻辑与“&amp;&amp;”、逻辑或“||”，具有短路效果：如果根据左边已经可以判断得到的最终结果，那么右边的代码将不再执行，从而节省一定的性能。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 短路</span></span><br><span class="line"><span class="keyword">int</span> i = <span class="number">3</span>;</span><br><span class="line"><span class="keyword">boolean</span> flag = (i &gt; <span class="number">3</span>) &amp;&amp; (++i &gt; <span class="number">3</span>);</span><br><span class="line">System.out.println(i); <span class="comment">// 3</span></span><br><span class="line">System.out.println(flag); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> j = <span class="number">3</span>;</span><br><span class="line"><span class="keyword">boolean</span> falg2 = (++j &gt; <span class="number">3</span>) &amp;&amp; (j &gt; <span class="number">3</span>);</span><br><span class="line">System.out.println(j); <span class="comment">// 4</span></span><br><span class="line">System.out.println(falg2); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>



<h3 id="6-5、位运算符"><a href="#6-5、位运算符" class="headerlink" title="6.5、位运算符"></a>6.5、位运算符</h3><ul>
<li>按位与 &amp;</li>
<li>按位或 |</li>
<li>异或 ^</li>
<li>取反 ~</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> b1 = <span class="number">0b11000011</span>;</span><br><span class="line"><span class="keyword">int</span> b2 = <span class="number">0b11001010</span>;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">  &amp; 同位上都是 1 则为 1 否则为 0</span></span><br><span class="line"><span class="comment">  | 同位上有 1 则为 1 否则为 0</span></span><br><span class="line"><span class="comment">  ^ 同位上相同为 0 不同为 1</span></span><br><span class="line"><span class="comment">  ~ 同位取反</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">System.out.println(Integer.toBinaryString(b1 &amp; b2)); <span class="comment">// 11000010</span></span><br><span class="line">System.out.println(Integer.toBinaryString(b1 | b2)); <span class="comment">// 11001011</span></span><br><span class="line">System.out.println(Integer.toBinaryString(b1 ^ b2)); <span class="comment">// 1001</span></span><br><span class="line"><span class="comment">// 11111111111111111111111111110110</span></span><br><span class="line">System.out.println(Integer.toBinaryString(~(b1 ^ b2))); </span><br></pre></td></tr></table></figure>

<ul>
<li>&lt;&lt; 左移：右边统一补 0 </li>
<li>&gt;&gt; 带符号右移：正数右移高位补 0，负数右移高位补 1</li>
<li>&gt;&gt;&gt; 无符号右移：无论正数还是负数右移，高位通通补 0</li>
<li>对于正数而言，&gt;&gt; 和 &gt;&gt;&gt; 没有区别</li>
<li>算数移位——有符号移位，逻辑移位——无符号移位</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    左移 &lt;&lt; 乘 2</span></span><br><span class="line"><span class="comment">    右移 &gt;&gt; 除 2</span></span><br><span class="line"><span class="comment">    00000010 2</span></span><br><span class="line"><span class="comment">    00000100 4 左移 1 次</span></span><br><span class="line"><span class="comment">    00001000 8 左移 2 次</span></span><br><span class="line"><span class="comment">    00010000 16 左移 3 次</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">System.out.println(<span class="number">2</span> &lt;&lt; <span class="number">3</span>); <span class="comment">// 16 </span></span><br><span class="line">System.out.println(<span class="number">16</span> &gt;&gt; <span class="number">2</span>); <span class="comment">// 4</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> a = -<span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> b = <span class="number">20</span>;</span><br><span class="line"><span class="comment">// 判断两个 int 型的数符号位是否相同</span></span><br><span class="line">System.out.println(((a &gt;&gt; <span class="number">31</span>) ^ (b &gt;&gt; <span class="number">31</span>)) == <span class="number">0</span>); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 【正数的有符号右移 &amp;&amp; 无符号右移】</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">有符号右移 &gt;&gt; 除于 2 向下取整</span></span><br><span class="line"><span class="comment">10</span></span><br><span class="line"><span class="comment">原码 0000_0000_0000_0000_0000_0000_0000_1010</span></span><br><span class="line"><span class="comment">补码 0000_0000_0000_0000_0000_0000_0000_1010</span></span><br><span class="line"><span class="comment">&gt;&gt;2  0000_0000_0000_0000_0000_0000_0000_0010</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">int</span> x = <span class="number">10</span>;</span><br><span class="line">System.out.println(x &gt;&gt; <span class="number">2</span>); <span class="comment">// 2</span></span><br><span class="line">System.out.println(Integer.toBinaryString(x &gt;&gt; <span class="number">2</span>)); <span class="comment">// 10</span></span><br><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment">无符号右移 &gt;&gt;&gt; 或 &gt;&gt;&gt;=</span></span><br><span class="line"><span class="comment">10</span></span><br><span class="line"><span class="comment">原码 0000_0000_0000_0000_0000_0000_0000_1010</span></span><br><span class="line"><span class="comment">补码 0000_0000_0000_0000_0000_0000_0000_1010</span></span><br><span class="line"><span class="comment">&gt;&gt;&gt;2 0000_0000_0000_0000_0000_0000_0000_0010</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line"><span class="keyword">int</span> z = <span class="number">10</span>;</span><br><span class="line">System.out.println(z &gt;&gt;&gt; <span class="number">2</span>); <span class="comment">// 2</span></span><br><span class="line">System.out.println(z &gt;&gt;&gt;= <span class="number">2</span>); <span class="comment">// 2</span></span><br><span class="line">System.out.println(Integer.toBinaryString(z &gt;&gt;&gt; <span class="number">2</span>)); <span class="comment">// 10</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 【负数的有符号右移 &amp;&amp; 无符号右移】</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">有符号右移 &gt;&gt; 除于 2 向下取整</span></span><br><span class="line"><span class="comment">-10</span></span><br><span class="line"><span class="comment">原码 1000_0000_0000_0000_0000_0000_0000_1010</span></span><br><span class="line"><span class="comment">补码 1111_1111_1111_1111_1111_1111_1111_0100</span></span><br><span class="line"><span class="comment">&gt;&gt;2  1111_1111_1111_1111_1111_1111_1111_1101</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">int</span> y = -<span class="number">10</span>;</span><br><span class="line">System.out.println(y &gt;&gt; <span class="number">2</span>); <span class="comment">// -3</span></span><br><span class="line"><span class="comment">// 11111111111111111111111111111101</span></span><br><span class="line">System.out.println(Integer.toBinaryString(y &gt;&gt; <span class="number">2</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 无符号右移 &gt;&gt;&gt; 或 &gt;&gt;&gt;=</span></span><br><span class="line"><span class="comment">/* -10</span></span><br><span class="line"><span class="comment">原码  1000_0000_0000_0000_0000_0000_0000_1010</span></span><br><span class="line"><span class="comment">补码  1111_1111_1111_1111_1111_1111_1111_0101</span></span><br><span class="line"><span class="comment">&gt;&gt;&gt;2  0011_1111_1111_1111_1111_1111_1111_1101</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">int</span> x = -<span class="number">10</span>;</span><br><span class="line">System.out.println(x &gt;&gt;&gt; <span class="number">2</span>); <span class="comment">// 1073741821</span></span><br><span class="line"><span class="comment">// 111111111111111111111111111101</span></span><br><span class="line">System.out.println(Integer.toBinaryString(x &gt;&gt;&gt; <span class="number">2</span>));</span><br></pre></td></tr></table></figure>



<h3 id="6-6、条件运算符"><a href="#6-6、条件运算符" class="headerlink" title="6.6、条件运算符"></a>6.6、条件运算符</h3><p>格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">数据类型 变量名称 = 条件判断 ? 条件为 <span class="keyword">true</span>: 条件为 <span class="keyword">false</span>;</span><br></pre></td></tr></table></figure>

<p><code>注意事项：</code></p>
<ul>
<li>必须同时保证表达式 A 和表达式 B 都符合左侧数据类型的要求。</li>
<li>三元运算符的结果必须被使用。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> temp = (<span class="number">3</span> &gt; <span class="number">2</span>) ? <span class="number">3</span> : <span class="number">2</span>;</span><br><span class="line">System.out.println(temp); <span class="comment">// 3</span></span><br></pre></td></tr></table></figure>



<h2 id="7、包机制"><a href="#7、包机制" class="headerlink" title="7、包机制"></a>7、包机制</h2><ul>
<li>为了更好的组织类，Java 提供了包机制，用于区别类名的命名空间</li>
<li>包语句的语法格式为：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pkg1 [.pkg2 [.pkg3……]];</span><br></pre></td></tr></table></figure>

<ul>
<li><code>一般用公司域名倒置作为包名</code></li>
<li>为了使用某一个包的成员，我们需要在 Java 程序中明确的导入该包，使用 “import” 语句可完成此功能。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> package1[.package2].(classname | *);</span><br></pre></td></tr></table></figure>



<h2 id="8、JavaDoc"><a href="#8、JavaDoc" class="headerlink" title="8、JavaDoc"></a>8、JavaDoc</h2><p>javadoc 命令是用来生成自己 API 文档的</p>
<p><strong>参数信息</strong></p>
<ul>
<li>@author作者名</li>
<li>@version版本号</li>
<li>@since指明需要最早使用的jdk版本</li>
<li>@param参数名</li>
<li>@return返回值情况</li>
<li>@throws异常抛出情况</li>
<li>doc 命令 生成 API 文档</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javadoc -encoding utf-<span class="number">8</span> -charset utf-<span class="number">8</span> 文件名.java</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.1</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span> 1.8</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> x 被除数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> y 除数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> the resule of x division y</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> ArithmeticException 算数异常</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">div</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> <span class="keyword">throws</span> ArithmeticException </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (x / y);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>学会查找使用 IDEA 生成 JavaDoc 文档！</p>
<h2 id="9、编译器的两点优化"><a href="#9、编译器的两点优化" class="headerlink" title="9、编译器的两点优化"></a>9、编译器的两点优化</h2><h3 id="9-1、-编译器的隐含强制类型转换"><a href="#9-1、-编译器的隐含强制类型转换" class="headerlink" title="9.1、 编译器的隐含强制类型转换"></a>9.1、 编译器的隐含强制类型转换</h3><p>对于 byte / short / char 三种类型来说，如果右侧赋值的数值没有超过范围，那么javac编译器将会自动隐含地为我们补上一个(byte) (short) (char)。这不是自动类型装换，而是强制类型装换，自动类型转换是：从小范围 –&gt; 大范围</p>
<ol>
<li>如果没有超过左侧的范围，编译器补上强转。</li>
<li>如果右侧超过了左侧范围，那么直接编译器报错。</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo01Notice</span></span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String [] args)</span></span>&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">// int --&gt; byte，不是自动类型转换</span></span><br><span class="line">		<span class="keyword">byte</span> num = <span class="comment">/*(byte)*/</span><span class="number">30</span>;</span><br><span class="line">		System.out.println(num); <span class="comment">// 30</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">// int --&gt; char，没有超过范围</span></span><br><span class="line">		<span class="comment">// 编译器将会自动补上一个隐含的(char)</span></span><br><span class="line">		<span class="keyword">char</span> zifu = <span class="comment">/*(char)*/</span><span class="number">65</span>;</span><br><span class="line">		System.out.println(zifu); <span class="comment">// A</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="9-2、-编译器的常量优化"><a href="#9-2、-编译器的常量优化" class="headerlink" title="9.2、 编译器的常量优化"></a>9.2、 编译器的常量优化</h3><p>在给变量进行赋值的时候，如果右侧的表达式当中全都是常量，没有任何变量，那么编译器javac将会直接将若干个常量表达式计算得到结果。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">short</span> result = <span class="number">5</span> + <span class="number">8</span>; <span class="comment">// 等号右边全都是常量，没有任何变量参与运算</span></span><br></pre></td></tr></table></figure>

<p>编译之后，得到的.class字节码文件当中相当于【直接就是】：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">short</span> result = <span class="number">13</span>; <span class="comment">// 右侧的常量结果数值，没有超过左侧范围，所以正确。</span></span><br></pre></td></tr></table></figure>

<p>这称为”编译器的常量优化“。<br>但是注意：一旦表达当中有变量参与，那么就不能进行这种优化了。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo02Notice</span></span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String [] args)</span></span>&#123;</span><br><span class="line">		<span class="keyword">short</span> a = <span class="number">5</span>;</span><br><span class="line">		<span class="keyword">short</span> b = <span class="number">8</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">// short result1 = a + b; // 错误写法！</span></span><br><span class="line">		<span class="comment">// System.out.println(result1);</span></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">short</span> result2 = <span class="number">5</span> + <span class="number">8</span>; <span class="comment">// 正确写法！</span></span><br><span class="line">		System.out.println(result2); <span class="comment">// 13</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
        </div>

        

        

        
            <div class="article-nav">
                
                
                    <div class="article-next">
                        <a class="next"
                           rel="next"
                           href="/2021/12/26/hello-world/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">Hello World</span>
                                <span class="post-nav-item">Next posts</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        

        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2020</span>
              -
            
            2021&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">SUNZJ</a>
        </div>
        
        <div class="theme-info info-item">
            Powered by <a target="_blank" href="https://hexo.io">Hexo</a>&nbsp;|&nbsp;Theme&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.4.5</a>
        </div>
        
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="fas fa-arrow-up"></i>
            </li>
        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
    </ul>
</div>

    </div>

    

    <div class="image-viewer-container">
    <img src="">
</div>


    

</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/header-shrink.js"></script>

<script src="/js/back2top.js"></script>

<script src="/js/dark-light-toggle.js"></script>








<div class="post-scripts">
    
</div>



</body>
</html>
